package com.google.android.gcm.demo.server;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MySQLAccess {
  private Connection connect = null;
  private Statement statement = null;
  private PreparedStatement preparedStatement = null;
  private ResultSet resultSet = null;
  
  public void open(){
	    try {
	        // This will load the MySQL driver, each DB has its own driver
	        Class.forName("com.mysql.jdbc.Driver");
	        // Setup the connection with the DB
	        connect = DriverManager
	            .getConnection("jdbc:mysql://sql2.freemysqlhosting.net:3306/sql28234?"
	                + "user=sql28234&password=iA2*xU4%25");
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
  }
  
  
  public void readDataBase() throws Exception {

	  open();
	  try{
     
		  

      preparedStatement = connect
          .prepareStatement("SELECT regId from sql28234.test");
      resultSet = preparedStatement.executeQuery();
      writeResultSet(resultSet);
      
     
      
    } catch (Exception e) {
      throw e;
    } finally {
      close();
    }

  }

  

  private void writeResultSet(ResultSet resultSet) throws SQLException {
    // ResultSet is initially before the first data set
    while (resultSet.next()) {
     
      String user = resultSet.getString("regId");
      Datastore.regIds.add(user);
      
      System.out.println("User: " + user);
     
    }
  }

  // You need to close the resultSet
  private void close() {
    try {
      if (resultSet != null) {
        resultSet.close();
      }

      if (statement != null) {
        statement.close();
      }

      if (connect != null) {
        connect.close();
      }
    } catch (Exception e) {

    }
  }

} 